package com.landian.commons.jpa.hql;


import com.google.common.collect.Lists;

import java.util.List;
import java.util.stream.Collectors;

/**
 * PredicateHqlOr
 *
 * @author caojl
 * @create 2017/12/06
 **/
public class PredicateHqlOr implements PredicateHql {

    private PredicateHqlOr(List<PredicateHql> predicateHqlList, ParameterUnit parameterUnit){
        this.predicateHqlList = predicateHqlList;
        this.parameterUnit = parameterUnit;
    }

    /**
     * predicateHql
     */
    private List<PredicateHql> predicateHqlList;

    public static PredicateHql newInstance(List<PredicateHql> predicateHqlList, ParameterUnit parameterUnit) {
        return new PredicateHqlOr(predicateHqlList, parameterUnit);
    }

    /**
     * name
     */
    private ParameterUnit parameterUnit;


    @Override
    public String hql() {
        List<String> hqlList = predicateHqlList.stream().map(b -> b.hql()).collect(Collectors.toList());
        String hql = "(" + String.join(" or ", hqlList) + ")";
        return hql;
    }

    @Override
    public ParameterUnit parameterUnit() {
        return parameterUnit;
    }


    public static void main(String[] args) {
        List<String> hqlList = Lists.newArrayList("u.loginName","u.userName","u.officePhone");
        String joinSQL = String.join(" or ", hqlList);
        System.out.println(joinSQL);
    }
}
